<?php


class ServiceOutletModel extends BaseModel
{
    protected $table = 'yf_service_outlet';

    protected $principal = 'id';

    const status = [
        'open' => 1,//开启
        'close' => -1,//关闭
    ];

    /**
     *按分类获取并根据经纬度计算距离排序
     *
     * @param int $category_id
     * @param int $page
     * @param int $pagesize
     * @param float $longitude
     * @param float $latitude
     * @return array
     */
    public function findListByCategoryOrderDistance(int $category_id, int $page, int $pagesize, float $longitude, float $latitude)
    {
        $sql = "select *,ROUND(6378.138 * 2 * ASIN( SQRT( POW( SIN( ( :latitude * PI() / 180 - latitude * PI() / 180 ) / 2 ), 2 ) + COS(:latitude * PI() / 180) * COS(latitude * PI() / 180) * POW( SIN( ( :longitude * PI() / 180 - longitude * PI() / 180 ) / 2 ), 2 ) ) ) * 1000 ) AS distance from {$this->table} where category_id=:category_id and status=:status order by distance asc limit :incept,:pagesize";
       return $this->db->query($sql, [
            ':latitude' => $latitude,
            ':longitude' => $longitude,
            ':category_id' => $category_id,
            ':status' => self::status['open'],
            ':incept' => ($page - 1) * $pagesize,
            ':pagesize' => $pagesize
        ])->fetchAll(PDO::FETCH_ASSOC);
    }
}